package com.example.tao_city.mymusicplay;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by TAO_CITY on 2018/2/28.
 */

public class MusicService extends Service {
    // 播放控制命令，标识操作
    public static final int COMMAND_UNKNOWN = -1;
    public static final int COMMAND_PLAY = 0;//播放
    public static final int COMMAND_PAUSE = 1;//暂停
    public static final int COMMAND_STOP = 2;//停止
    public static final int COMMAND_RESUME = 3;//继续播放
    public static final int COMMAND_PREVIOUS = 4;//上一首
    public static final int COMMAND_NEXT = 5;//下一首
    public static final int COMMAND_CHECK_IS_PLAYING = 6;
    public static final int COMMAND_SEEK_TO = 7;//将歌曲调至该进度
    // 播放器状态̬
    public static final int STATUS_PLAYING = 0;//正在播放
    public static final int STATUS_PAUSED = 1;//暂停
    public static final int STATUS_STOPPED = 2;//停止
    public static final int STATUS_COMPLETED = 3;//重新播放
    // 广播标志
    public static final String BROADCAST_MUSICSERVICE_CONTROL = "MusicService.ACTION_CONTROL";
    public static final String BROADCAST_MUSICSERVICE_UPDATE_STATUS = "MusicService.ACTION_UPDATE";

    // 广播接收器
    private CommandReceiver receiver;
    // 媒体播放类
    private MediaPlayer player = new MediaPlayer();
    //歌曲序号，从0开始
    private int number = 0;
    private int status;



    @Override
    public void onCreate() {
        super.onCreate();
        bindCommandReceiver();
    }
    @Override
    public void onDestroy() {
        if (player != null) {
            player.release();
        }
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    // 绑定广播接收器
    private void bindCommandReceiver() {
        receiver = new CommandReceiver();
        IntentFilter filter = new IntentFilter(BROADCAST_MUSICSERVICE_CONTROL);
        registerReceiver(receiver, filter);
    }
    //内部类接受广播信息
    class CommandReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取命令
            int command = intent.getIntExtra("command", COMMAND_UNKNOWN);
            switch (command){
                case COMMAND_SEEK_TO:
                    seekTo(intent.getIntExtra("time",0));
                    break;
                case COMMAND_PLAY:
                    number = intent.getIntExtra("number", 0);
                    play(number);
                    break;
                case COMMAND_PREVIOUS:
                    moveNumberToPrevious();
                    break;
                case COMMAND_NEXT:
                    moveNumberToNext();
                    break;
                case COMMAND_PAUSE:
                    pause();
                    break;
                case COMMAND_STOP:
                    stop();
                    break;
                case COMMAND_RESUME:
                    resume();
                    break;
                case COMMAND_CHECK_IS_PLAYING:
                    if (player != null && player.isPlaying()) {
                        sendBroadcastOnStatusChanged(MusicService.STATUS_PLAYING);
                    }
                    break;
                case COMMAND_UNKNOWN:
                default:
                    break;
            }
        }
    }

    //发送广播，提醒状态改变了
    private void sendBroadcastOnStatusChanged(int status) {
        Intent intent = new Intent(BROADCAST_MUSICSERVICE_UPDATE_STATUS);
        intent.putExtra("status", status);
        if(status!=STATUS_STOPPED){
            intent.putExtra("time",player.getCurrentPosition());//当前时间
            intent.putExtra("duration",player.getDuration());//总时间
            intent.putExtra("number",number);//歌曲位置
            intent.putExtra("musicnName",Musiclist.getMusiclist().get(number).getMusicName());//歌名
            intent.putExtra("musicArtist",Musiclist.getMusiclist().get(number).getMusicArtist());//歌手
        }
        sendBroadcast(intent);
    }
    //读取音乐文件
    private void load(int number) {
        try {
            player.reset();
            player.setDataSource(Musiclist.getMusiclist().get(number).getMusicPath());
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //注册监听器
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                if(mediaPlayer.isLooping()){
                    replay();
                }else {
                    sendBroadcastOnStatusChanged(MusicService.STATUS_COMPLETED);
                }
            }
        });
    }
    //选择下一首
    private void moveNumberToNext() {
        // 判断是否到达列表底端
        if ((number ) == Musiclist.getMusiclist().size()-1) {
            Toast.makeText(MusicService.this,"已经到达列表底部",Toast.LENGTH_SHORT).show();
        } else {
            ++number;
            play(number);
        }
    }

    //选择上一首
    private void moveNumberToPrevious() {
        // 判断是否到达列表顶端
        if (number == 0) {
            Toast.makeText(MusicService.this,"已达到列表顶端",Toast.LENGTH_SHORT).show();
        } else {
            --number;
            play(number);
        }
    }
    //播放音乐
    private void play(int number) {
        // 停止当前播放
        if (player != null && player.isPlaying()) {
            player.stop();
        }
        load(number);
        player.start();
        status = MusicService.STATUS_PLAYING;
        sendBroadcastOnStatusChanged(MusicService.STATUS_PLAYING);
    }
    //暂停音乐
    private void pause() {
        if (player.isPlaying()) {
            player.pause();
            status = MusicService.STATUS_PAUSED;
            sendBroadcastOnStatusChanged(MusicService.STATUS_PAUSED);
        }
    }

    // 停止播放
    private void stop() {
        if (status != MusicService.STATUS_STOPPED) {
            player.stop();
            status = MusicService.STATUS_STOPPED;
            sendBroadcastOnStatusChanged(MusicService.STATUS_STOPPED);
        }
    }

    //恢复播放（暂停之后）
    private void resume() {
        player.start();
        status = MusicService.STATUS_PLAYING;
        sendBroadcastOnStatusChanged(MusicService.STATUS_PLAYING);
    }

    // 重新播放（播放完成之后）
    private void replay() {
        player.start();
        status = MusicService.STATUS_PLAYING;
        sendBroadcastOnStatusChanged(MusicService.STATUS_PLAYING);
    }
    private void seekTo(int time){
        player.seekTo(time);
    }
    public MediaPlayer getPlay(){
        return player;
    }

}
